package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "APP购物车管理")
@Slf4j
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    // 添加购物车
    @ApiOperation(value = "添加购物车")
    @PostMapping("/add")
    public ResultInfo adds(@RequestBody Cart cartParam) {// 1.接收参数
        // 2.调用service添加
        Cart cart = cartService.add(cartParam);
        // 3.返回结果
        return ResultInfo.success(cart);
    }

    //查看购物车列表
    @ApiOperation(value = "查看购物车列表")
    @GetMapping("/list")
    public ResultInfo lists(){

        // 1.调用service查询
        List<Cart> list = cartService.listCart();

        // 2.返回结果
        return ResultInfo.success(list);
    }

    //清空购物车
    @ApiOperation(value = "清空购物车")
    @DeleteMapping("/clean")
    public ResultInfo clean(){

        cartService.cleanCart();

        return ResultInfo.success();
    }

    // 修改购物车 不需要回显具体数据，直接改数量
    @ApiOperation(value = "修改购物车")
    @PostMapping("/sub")
    public ResultInfo sub(@RequestBody Cart cartParam){

    Cart cart =cartService.subCart(cartParam);

    return ResultInfo.success(cart);

    }


}
